using Jacobi.Vst.Core;
using Jacobi.Vst.Interop.Host;

namespace Play.AudioEngine
{
	public class Channel
	{
		private readonly VstPluginContext _pluginContext;
		private VstAudioBuffer[] _vstInputBuffers;
		private VstAudioBuffer[] _vstOutputBuffers;

		public Channel(VstPluginContext pluginContext)
		{
			_pluginContext = pluginContext;

			_vstInputBuffers = new VstAudioBufferManager(
				AudioParameters.NbChannels, AudioParameters.BlockSize).ToArray();
			_vstOutputBuffers = new VstAudioBufferManager(
				AudioParameters.NbChannels, AudioParameters.BlockSize).ToArray();

			_pluginContext.PluginCommandStub.SetBlockSize(1);
			_pluginContext.PluginCommandStub.SetSampleRate(AudioParameters.SampleRate);
			_pluginContext.PluginCommandStub.SetProcessPrecision(VstProcessPrecision.Process32);
			_pluginContext.PluginCommandStub.StartProcess();
			_pluginContext.PluginCommandStub.MainsChanged(true);
		}

		public VstAudioBuffer[] ProcessAudio()
		{
			_pluginContext.PluginCommandStub.ProcessReplacing(_vstInputBuffers, _vstOutputBuffers);
			return _vstOutputBuffers;
		}

		public void ProcessMidi(VstEvent[] vstEvents)
		{
			_pluginContext.PluginCommandStub.ProcessEvents(vstEvents);
		}
	}
}